package com.yjt.fresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseResultEntity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyGetCoupons;
import com.yjt.fresh.api.ApiGetStoreCoupon;
import com.yjt.fresh.api.ApiStoreCoupons;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanStoreCoupon;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.PreferenceManager;
import com.yjt.fresh.widget.CustomToastResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 领取优惠券
 * Created by mcc on 2018-07-24.
 */

public class AtyGetCoupon extends BaseSimpleActivity {
    private static final int PAGE_SIZE = 10;
    @BindView(R.id.back)
    RelativeLayout mBack;
    @BindView(R.id.rvAppAtyGetCoupon)
    RecyclerView mRv;
    @BindView(R.id.srlAppAtyGetCoupon)
    SwipeRefreshLayout mRefreshLayout;

    private int mNextRequestPage = 1;
    private boolean isLoadMore;
    private AdpRvAtyGetCoupons mAdpGetCoupons;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_get_coupon;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initEvent() {
        initRV();
        getData();
        initRefresh();
        initClick();
    }

    private void initClick() {
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mAdpGetCoupons.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tvAppRecycleItemAtyGetCouponOperation){
                    if ("1".equals(mAdpGetCoupons.getData().get(position).getISGET())){
                        if (!TextUtils.isEmpty(getIntent().getStringExtra(MyConstants.STORE_ID))) {
                            finish();
                        }else {
                            BeanStoreCoupon beanStoreCoupon = mAdpGetCoupons.getData().get(position);
                            Intent intent = new Intent(mContext,AtyStoreDetail.class);
                            intent.putExtra(MyConstants.STORE_ID,beanStoreCoupon.getOWNER());
                            intent.putExtra(MyConstants.STORE_NAME,beanStoreCoupon.getSTORE_NAME());
                            intent.putExtra(MyConstants.STORE_LOGO,beanStoreCoupon.getLOGO_IMG());
                            intent.putExtra(MyConstants.STORE_LOCATION,beanStoreCoupon.getLATITUDE_LONGITUDE());
                            intent.putExtra(MyConstants.STORE_PHONE,beanStoreCoupon.getTEL());
                            startActivity(intent);
                        }
                    }else if ("0".equals(mAdpGetCoupons.getData().get(position).getISGET())){
                        getCoupon(mAdpGetCoupons.getData().get(position).getCOUPON_ID());
                    }
                }
            }
        });
    }


    private void initRV() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mRv.setLayoutManager(linearLayoutManager);
        List<BeanStoreCoupon> beanCoupons = new ArrayList<>();
        mAdpGetCoupons = new AdpRvAtyGetCoupons(R.layout.app_recycle_item_coupon, beanCoupons);
        mRv.setAdapter(mAdpGetCoupons);
    }


    HttpOnNextListener<List<BeanStoreCoupon>> mListener = new HttpOnNextListener<List<BeanStoreCoupon>>() {
        @Override
        public void onNext(List<BeanStoreCoupon> beanCoupons) {
            mAdpGetCoupons.setEmptyView(R.layout.view_empty);
            if (isLoadMore) {
                setData(false, beanCoupons);
            } else {
                setData(true, beanCoupons);
                mRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void setData(boolean isRefresh, List<BeanStoreCoupon> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdpGetCoupons.setNewData(data);
        } else {
            if (size > 0) {
                mAdpGetCoupons.addData(data);
                mAdpGetCoupons.loadMoreComplete();
            }else {
                mAdpGetCoupons.loadMoreEnd(true);
            }
        }
    }

    private void getData() {
        ApiStoreCoupons apiStoreCoupons = new ApiStoreCoupons(mListener, mContext, true, false, false);
        Map<String, Object> map = new HashMap<>();
        map.put("currentPage", mNextRequestPage);
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        if (!TextUtils.isEmpty(getIntent().getStringExtra(MyConstants.STORE_ID))) {
            map.put("STORE_ID", getIntent().getStringExtra(MyConstants.STORE_ID));
        }
        apiStoreCoupons.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiStoreCoupons);
    }

    private void initRefresh() {
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNextRequestPage = 1;
                isLoadMore = false;
                getData();
            }
        });
        mAdpGetCoupons.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isLoadMore = true;
                getData();
            }
        }, mRv);


    }

    HttpOnNextListener<BaseResultEntity> mGetStoreCouponListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            CustomToastResult.show("领取成功");
            mNextRequestPage = 1;
            isLoadMore = false;
            getData();
        }

        @Override
        public void onReLogin() {

        }
    };
    private void getCoupon(String coupon_id) {
        ApiGetStoreCoupon apiGetStoreCoupon = new ApiGetStoreCoupon(mGetStoreCouponListener,mContext,true,false,false);
        Map<String,Object> map = new HashMap<>();
        map.put("COUPON_ID",coupon_id);
        map.put("SESSION_ID",PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        apiGetStoreCoupon.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetStoreCoupon);
    }
}
